package cn.edu.cqut.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.edu.cqut.entity.EcharsServeModel;
import cn.edu.cqut.entity.Serve;
import cn.edu.cqut.service.IServeService;
import cn.edu.cqut.util.PageResult;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cqut-917371
 * @since 2020-06-24
 */
@RestController
@RequestMapping("/serve")
@CrossOrigin // 允许跨域请求
public class ServeController {
	@Autowired
	private IServeService serveService;

	@RequestMapping("/createServe")
	public PageResult<Serve> createServe(Serve serve) {
		serveService.save(serve);
		PageResult<Serve> pr = new PageResult<>();
		pr.setCode(0);
		pr.setMsg("创建服务成功");
		return pr;
	}

	@RequestMapping("/getServeList")
	public PageResult<Serve> getServeList(@RequestParam(defaultValue = "1") Integer page, // RequestParam设置默认请求

			@RequestParam(defaultValue = "10") Integer limit, Serve serve) {
		QueryWrapper<Serve> queryWapper = new QueryWrapper<>();
		if (serve.getSeState() == 0) {
			queryWapper.eq("se_state", 0);
		}
		Page<Serve> page3 = new Page<Serve>(page, limit);
		Page<Serve> page2 = serveService.page(page3, queryWapper);
		PageResult<Serve> pr = new PageResult<>();
		pr.setCode(0);
		pr.setCount(page2.getTotal());
		pr.setMsg("");
		pr.setData(page2.getRecords());
		return pr;
	}

	/*
	 * @RequestMapping("/getServeList1") public PageResult<Serve>
	 * getCustomerList(@RequestParam(defaultValue = "1") Integer page, //
	 * RequestParam设置默认请求
	 * 
	 * @RequestParam(defaultValue = "10") Integer limit, Serve serve, String
	 * serveType, String serveSas,String seState, String serveCreator) {
	 * QueryWrapper<Serve> queryWrapper = new QueryWrapper<>(); if (serveType !=
	 * null && !"".equals(serveType)) { queryWrapper.inSql("se_type", serveType); }
	 * 
	 * if (serveSas != null && !"".equals(serveSas)) {
	 * queryWrapper.inSql("se_statisficing", serveSas); }
	 * 
	 * if (serveCreator != null && !"".equals(serveCreator)) {
	 * queryWrapper.inSql("se_creator", serveCreator); } // QueryWrapper<Serve>
	 * queryWapper = new QueryWrapper;<>() if (serve.getSeState() == 3) {
	 * queryWrapper .eq("se_state", 3); }
	 */
	//标识 http 请求地址与 Controller 类的方法之间的映射。
	@RequestMapping("/getServeList1")
		public PageResult<Serve> getCustomerList(@RequestParam(defaultValue = "1") Integer page, // RequestParam设置默认请求
				@RequestParam(defaultValue = "10")//一页最多显示10条数据
	                                                     Integer limit, Serve serve, String serveType, String serveSas,String seState,
				String serveCreator) {
			QueryWrapper<Serve> queryWrapper = new QueryWrapper<>();//自定义条件查询
			if (serveType != null && !"".equals(serveType)) {
				queryWrapper.inSql("se_type", serveType);
			}

			if (serveSas != null && !"".equals(serveSas)) {
				queryWrapper.inSql("se_statisficing", serveSas);
			}

			if (serveCreator != null && !"".equals(serveCreator)) {
				queryWrapper.inSql("se_creator", serveCreator);
			}
//			QueryWrapper<Serve> queryWapper = new QueryWrapper;<>()
	                                  //获取服务状态为3的数据
			if (serve.getSeState() == 3) {
				queryWrapper .eq("se_state", 3);
			}
		Page<Serve> page2 = serveService.page(new Page<Serve>(page, limit), queryWrapper);
		PageResult<Serve> pr = new PageResult<>();
		pr.setCode(0);
		pr.setCount(page2.getTotal());
		pr.setMsg("");
		pr.setData(page2.getRecords());
		return pr;
	}

	@RequestMapping("/distributeServe")
	public PageResult<Serve> distributeServe(Serve serve) {
		serveService.updateById(serve);
		PageResult<Serve> pr = new PageResult<>();
		pr.setMsg("分配服务成功！");
		pr.setCode(0);
		return pr;
	}

	@RequestMapping("/getServeProcessList")
	public PageResult<Serve> getServeProcessList(@RequestParam(defaultValue = "1") Integer page, // RequestParam设置默认请求
			@RequestParam(defaultValue = "10") Integer limit, Serve serve) {
		QueryWrapper<Serve> queryWapper = new QueryWrapper<>();
		queryWapper.eq("se_state", 1);
		Page<Serve> page3 = new Page<Serve>(page, limit);
		Page<Serve> page2 = serveService.page(page3, queryWapper);
		PageResult<Serve> pr = new PageResult<>();
		pr.setCode(0);
		pr.setCount(page2.getTotal());
		pr.setMsg("");
		pr.setData(page2.getRecords());
		return pr;

	}

	@RequestMapping("/serveProcess")
	public PageResult<Serve> serveProcess(Serve serve) {
		serveService.updateById(serve);
		PageResult<Serve> pr = new PageResult<>();
		pr.setCode(0);
		pr.setMsg("服务处理成功");
		return pr;
	}

	@RequestMapping("/getServeFeedbackList")
	public PageResult<Serve> getServeLFeedbackList(@RequestParam(defaultValue = "1") Integer page, // RequestParam设置默认请求
			@RequestParam(defaultValue = "10") Integer limit, Serve serve) {
		QueryWrapper<Serve> queryWapper = new QueryWrapper<>();
		queryWapper.eq("se_state", 2);
		Page<Serve> page3 = new Page<Serve>(page, limit);
		Page<Serve> page2 = serveService.page(page3, queryWapper);
		PageResult<Serve> pr = new PageResult<>();
		pr.setCode(0);
		pr.setCount(page2.getTotal());
		pr.setMsg("");
		pr.setData(page2.getRecords());
		return pr;

	}

	@RequestMapping("/serveFeedback")
	public PageResult<Serve> serveFeedback(Serve serve) {
		serveService.updateById(serve);
		PageResult<Serve> pr = new PageResult<>();
		pr.setCode(0);
		pr.setMsg("服务反馈成功");
		return pr;
	}
	
	@RequestMapping("/getAllServe")
	public List<Object> getAllServe() {
		List<Object> res = new ArrayList<>();
		List<EcharsServeModel> serve = serveService.getAllServe();
		List<Object> type0 = new ArrayList<>();
		List<Object> type1 = new ArrayList<>();
		List<Object> type2 = new ArrayList<>();
		List<Object> title = new ArrayList<>();
		title.add("product");
		type0.add("咨询");
		type1.add("投诉");
		type2.add("建议");
		for (EcharsServeModel rm : serve) {
			Boolean a = true;
			Boolean b = true;
			Boolean c = true;
			String date = rm.getDate();
			if (!title.contains(date)) {
				title.add(date);
				for (EcharsServeModel m : serve) {
					if (m.getDate().equals(date)) {
						int type = (int)m.getType();
						switch (type) {
							case 0:
								a = false;
								type0.add(m.getNumbers());
								break;
							case 1:
								b = false;
								type1.add(m.getNumbers());
								break;
							case 2:
								c = false;
								type2.add(m.getNumbers());
								break;
						}
					}	
				}
				if (a) {
					type0.add(0);
				}
				if (b) {
					type1.add(0);
				}
				if (c) {
					type2.add(0);
				}
				
			}
			
		}
		res.add(title);
		res.add(type0);
		res.add(type1);
		res.add(type2);
		return res;
	}
}
